package com.fruit.service.fruit;

import com.fruit.bean.constant.factory.PageFactory;
import com.fruit.bean.dto.fruit.FruitUserOrderInfoDto;
import com.fruit.bean.entity.fruit.FruitShelfInfo;
import com.fruit.bean.entity.fruit.FruitUserAddressInfo;
import com.fruit.bean.entity.fruit.FruitUserOrderInfo;
import com.fruit.bean.entity.system.User;
import com.fruit.bean.enumeration.fruit.FruitUserOrderStatusEnum;
import com.fruit.bean.vo.query.DynamicSpecifications;
import com.fruit.bean.vo.query.SearchFilter;
import com.fruit.dao.fruit.FruitShelfRepository;
import com.fruit.dao.fruit.FruitUserAddressRepository;
import com.fruit.dao.fruit.FruitUserOrderRepository;
import com.fruit.dao.system.UserRepository;
import com.fruit.service.BaseService;
import com.fruit.utils.BigDecimalUtils;
import com.fruit.utils.factory.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class FruitUserOrderService extends BaseService<FruitUserOrderInfo,Long, FruitUserOrderRepository> {

    @Autowired
    private FruitUserOrderRepository fruitUserOrderRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FruitCommentService commentService;

    @Autowired
    private FruitShelfRepository shelfRepository;

    @Autowired
    private FruitUserAddressRepository userAddressRepository;

    public Page<FruitUserOrderInfoDto> queryUserOrderPage() {
        Page<FruitUserOrderInfoDto> pageRes = new PageFactory().defaultPage();
        Page<FruitUserOrderInfo> page = new PageFactory().defaultPage();
        Pageable pageable;
        if (page.getSort() != null) {
            pageable = PageRequest.of(page.getCurrent() - 1, page.getSize(), page.getSort());
        } else {
            pageable = PageRequest.of(page.getCurrent() - 1, page.getSize(), Sort.Direction.DESC, "id");
        }
        Specification<FruitUserOrderInfo> specification = DynamicSpecifications.bySearchFilter(
                page.getFilters(), fruitUserOrderRepository.getDataClass());
        org.springframework.data.domain.Page<FruitUserOrderInfo> pageResult = fruitUserOrderRepository.findAll(specification, pageable);
        List<FruitUserOrderInfo> fruitUserOrderInfoList = pageResult.getContent();
        List<FruitUserOrderInfoDto> fruitUserOrderInfoDtoList = convertUserOrderInfoDto(fruitUserOrderInfoList);
        pageRes.setTotal(Integer.valueOf(pageResult.getTotalElements() + ""));
        pageRes.setRecords(fruitUserOrderInfoDtoList);
        return pageRes;
    }

    public FruitUserOrderInfo updateOrderInfo(FruitUserOrderInfo userOrderInfo){
        return fruitUserOrderRepository.save(userOrderInfo);
    }

    public List<FruitUserOrderInfoDto> listUserOrderInfo(Long userId){
        SearchFilter filter = SearchFilter.build("userId", SearchFilter.Operator.EQ, userId);
        List<FruitUserOrderInfo> userOrderInfoList = this.queryAll(filter);
        return convertUserOrderInfoDto(userOrderInfoList);
    }

    private List<FruitUserOrderInfoDto> convertUserOrderInfoDto(List<FruitUserOrderInfo> userOrderInfoList){
        List<FruitUserOrderInfoDto> fruitUserOrderInfoDtoList = new ArrayList<>();
        for(FruitUserOrderInfo fruitUserOrderInfo : userOrderInfoList){
            FruitUserOrderInfoDto fruitUserOrderInfoDto = new FruitUserOrderInfoDto();
            fruitUserOrderInfoDto.setUserOrderId(fruitUserOrderInfo.getId());
            Optional<User> user = userRepository.findById(fruitUserOrderInfo.getUserId());
            fruitUserOrderInfoDto.setUserName(user.get().getAccount());
            fruitUserOrderInfoDto.setUserOrderNumber(fruitUserOrderInfo.getUserOrderNumber());
            fruitUserOrderInfoDto.setUserOrderPrice(fruitUserOrderInfo.getUserOrderPrice());
            fruitUserOrderInfoDto.setUserOrderTime(fruitUserOrderInfo.getUserOrderTime());
            // 判断是否已经评价
            SearchFilter filter = SearchFilter.build("fruitOrderId", SearchFilter.Operator.EQ,
                    fruitUserOrderInfo.getId());
            if(commentService.get(filter) == null){
                fruitUserOrderInfoDto.setHasComment(0);
            }else{
                fruitUserOrderInfoDto.setHasComment(1);
            }
            //获取商品图片
            FruitShelfInfo shelfInfo = shelfRepository.getOne(fruitUserOrderInfo.getFruitShelfId());
            if(StringUtils.isNotEmpty(shelfInfo.getFruitShelfImgs())) {
                fruitUserOrderInfoDto.setUserOrderPic(shelfInfo.getFruitShelfImgs().split(";")[0]);
            }
            fruitUserOrderInfoDto.setShelfName(shelfInfo.getFruitShelfName());
            fruitUserOrderInfoDto.setUserOrderPay(BigDecimalUtils.mul(fruitUserOrderInfoDto.getUserOrderNumber(),
                    fruitUserOrderInfoDto.getUserOrderPrice()).doubleValue());
            fruitUserOrderInfoDto.setUserOrderStatus(fruitUserOrderInfo.getUserOrderStatus());

            //获取收件人信息
            FruitUserAddressInfo userAddressInfo = userAddressRepository.getOne(fruitUserOrderInfo.getUserAddressId());
            fruitUserOrderInfoDto.setReceiveUserName(userAddressInfo.getUserName());
            fruitUserOrderInfoDto.setReceiveUserPhone(userAddressInfo.getUserPhone());
            fruitUserOrderInfoDto.setReceiveUserAddress(userAddressInfo.getUserAddress());
            fruitUserOrderInfoDtoList.add(fruitUserOrderInfoDto);
        }
        return fruitUserOrderInfoDtoList;
    }

    /**
     * 获取所有没有收货的订单
     * @return 订单列表
     */
    public List<FruitUserOrderInfo> listUnRecievedOrder(){
        return fruitUserOrderRepository.queryUnRecievedOrder(FruitUserOrderStatusEnum.PAID.getCode(),
                FruitUserOrderStatusEnum.TRANSFORM.getCode());
    }
}
